package com.fdb.efp.esb.service.mock.zx;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fdb.efp.esb.common.constant.ESBTradeNoConstance;
import com.fdb.efp.esb.service.bo.req.BaseZXReqBean;
import com.fdb.efp.esb.service.bo.req.ZX11001QueryReports;
import com.fdb.efp.esb.service.bo.resp.zx.BaseZXResBean;
import com.fdb.efp.esb.service.bo.resp.zx.ZX11001ResponseReport;
import com.fdb.efp.esb.service.vo.zx.ZXVO;
import com.fdb.basic.framework.core.exception.ESBException;

/**
 * @description:黑名单接口
 * @date:2018年12月20日 下午2:30:14
 * @author:hengyh
 */
@Service("zx11001Service")
@DependsOn("ZXConfig")// 确保ZXConfig配置类bean先被容器创建
public class ZX11001ServiceImpl extends BaseZXService<ZX11001QueryReports, ZX11001ResponseReport> {

	private static Logger log = LoggerFactory.getLogger(ZX11001ServiceImpl.class);

	public static final String INTERFACE_ID = "11001";
	public static final String QUERY_TYPE = "A"; // 卡中心暂时只支持“A"

	@Override
	protected void preProcess(BaseZXReqBean<ZX11001QueryReports> reqBean) {
		ZX11001QueryReports queryReports = reqBean.getQryRptsInfArry().get(0);
		queryReports = new ZX11001QueryReports.Builder()
				.buildWithExists(queryReports)
				.withReportId(INTERFACE_ID)
				.withQueryType(QUERY_TYPE)
				.withCertType("0")
				.build();
		log.debug("进入黑名单查询接口，参数：" + Objects.toString(queryReports));
	}

	@Override
	protected ZXVO postProcess(BaseZXReqBean<ZX11001QueryReports> reqBean,
							   BaseZXResBean<ZX11001ResponseReport> resBean) throws ESBException {
//		Optional.ofNullable(resBean.getRepRptsInfArry()).map(i -> i.get(0))
//			.orElseThrow(() -> new ESBException("黑名单接口查询返回内容为空！"));
//		if (log.isDebugEnabled()) 
//			log.debug("黑名单查询接口,进行后处理，请求参数： " + reqBean + ", 返回结果： " + resBean);
		List<ZX11001ResponseReport> list = new ArrayList<ZX11001ResponseReport>();
		list.add(new ZX11001ResponseReport());
		return new ZXVO(Boolean.TRUE, list);
	}

	@Override
	protected String tradeNo() {
		return ESBTradeNoConstance.ZX11001;
	}

	@Override
	protected String scene() {
		return "01";
	}

	@Override
	protected Function<String, BaseZXResBean<ZX11001ResponseReport>> converter() {
		return i -> JSONObject.parseObject(i, new TypeReference<BaseZXResBean<ZX11001ResponseReport>>() {});
	}
}
